package com.googlecode.animator.view.viewselector
{
	import com.googlecode.animator.data.Notifications;
	import com.googlecode.animator.data.cameras.ActualViewCameraVO;
	import com.googlecode.animator.data.cameras.CameraVO;
	import com.googlecode.animator.data.cameras.MainViewCameraVO;
	import com.googlecode.animator.data.viewports.ViewportVO;
	import com.googlecode.animator.model.ProjectSettingsProxy;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.ComboBox;
	import mx.events.ListEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.mediator.Mediator;

	/**
	 * Handles the ViewSelector component, used to change a view of the 3D world. 
	 * @author USER
	 * 
	 */
	public class ViewSelectorMediator extends Mediator
	{
		/**
		 * Identifier.  
		 */
		public static const NAME:String = "ViewSelectorMediator";
		/**
		 * Used to append number to identifier to make it unique. 
		 */
		public static var count:int = 0;
		
		protected var _camerasCollection:ArrayCollection;
		
		protected var _currentCamera:String;
		
		/**
		 *  
		 * @param mediatorName
		 * @param viewComponent
		 * 
		 */
		public function ViewSelectorMediator(viewComponent:ComboBox)
		{
			var name:String = NAME + "_" + count;
			
			super(name, viewComponent);
			
			count ++;
		}
		
		/**
		 * Triggered by PureMVC framework. 
		 * 
		 */
		override public function onRegister():void
		{
			viewRef.addEventListener(ListEvent.CHANGE, onSelectorChange);
			viewRef.addEventListener(Event.REMOVED, onSelectorRemoved);
			_camerasCollection = new ArrayCollection(); 
			
		}
		
		/**
		 * Tidy up. 
		 * 
		 */
		override public function onRemove():void
		{
			viewRef.removeEventListener(ListEvent.CHANGE, onSelectorChange);
			viewRef.removeEventListener(Event.REMOVED, onSelectorRemoved);
			_camerasCollection = null;
		}
		/**
		 * Lists interest in notifications.
		 * @return 
		 * 
		 */
		override public function listNotificationInterests():Array
		{
			var list:Array = super.listNotificationInterests();
			
			list.push(Notifications.VIEWPORT_ADDED);
			//list.push(Notifications.CAMERA_ADDED);
			//list.push(Notifications.RESET_PROJECT);
			list.push(Notifications.DEFAULT_CAMERAS_ADDED);
			//list.push(Notifications.DEFAULT_VIEWPORTS_ADDED);
			
			return list;
		}
		
		/**
		 * Handles notifications.
		 * @param notification
		 * 
		 */
		override public function handleNotification(notification:INotification):void
		{
			super.handleNotification(notification);
			
			switch(notification.getName())
			{
					
				/* case Notifications.CAMERA_ADDED:
					
					loadData(notification.getBody() as CameraVO);	
					
					break; */
					
				/* case Notifications.RESET_PROJECT:
					
					handleResetProject();	
					
					break; */
				
				case Notifications.VIEWPORT_ADDED:
					
					handleViewportAdded(notification.getBody() as ViewportVO);	
					
					break;
					  
				/* case Notifications.DEFAULT_VIEWPORTS_ADDED:
					
					handleDefaultViewportsAdded();	
					
					break;  */
				case Notifications.DEFAULT_CAMERAS_ADDED:
					
					handleDefaultCamerasAdded();	
					
					break;
			}
		}
		
		protected function handleDefaultCamerasAdded():void
		{
			_camerasCollection = new ArrayCollection();
			
			for each(var cameraVO:CameraVO in settings.cameras)
			{
				if(cameraVO is ActualViewCameraVO)
				{
					_camerasCollection.addItem((cameraVO as ActualViewCameraVO).clone());
	
				}
				
				if(cameraVO is MainViewCameraVO)
				{
					_camerasCollection.addItem((cameraVO as MainViewCameraVO).clone());
					
				}
			}
			
			// assign array collection to dp
			(viewRef as ComboBox).dataProvider = _camerasCollection;
			
			// choose selected camera
			for(var i:uint=0; i<_camerasCollection.length; i++)
			{
				if((_camerasCollection.getItemAt(i) as CameraVO).name == _currentCamera)
				{
					(viewRef as ComboBox).selectedIndex = i;

					break;
				}
			}
		}
		
		protected function handleViewportAdded(vVO:ViewportVO):void
		{
			if(vVO.viewportID == viewRef.viewportID)
			{
				_currentCamera = vVO.currentCamera;
			}
			
		} 
		
		/**
		 * Once all the viewports have been added it is safe to carry out
		 * operations that apply to all of the viewports.
		 * 
		 */
		/* protected function handleDefaultViewportsAdded():void
		{
			// set the current camera
			_currentCamera = settings.getViewport(viewRef.viewportID).currentCamera;
		} */ 
		
		/* protected function handleResetProject():void
		{
			// make array collection
			_camerasCollection = new ArrayCollection();
		} */
		
		/**
		 * Responds to users changing listed view camera. 
		 * @param evt
		 * 
		 */
		protected function onSelectorChange(evt:ListEvent):void
		{
			var vs:ViewSelector = (evt.target as ViewSelector);

			sendNotification(Notifications.VIEW_SELECTOR_CHANGED, vs);
		}
		
		/**
		 * Responds to the removal of the view selector component. 
		 * @param evt
		 * 
		 */
		protected function onSelectorRemoved(evt:Event):void
		{
			facade.removeMediator(getMediatorName());
		}
		
		//////////////////////////////////////////////////////////////////////
		//
		// Utility functions
		//
		//////////////////////////////////////////////////////////////////////
		
		/**
		 * Returns typed reference to the model. 
		 * @return 
		 * 
		 */
		protected function get settings():ProjectSettingsProxy
		{
			var proxy:IProxy = facade.retrieveProxy(ProjectSettingsProxy.NAME);
			
			return proxy as ProjectSettingsProxy;
		}
		
		/**
		 * Returns typed reference to the component. 
		 * @return 
		 * 
		 */
		protected function get viewRef():ViewSelector
		{
			return viewComponent as ViewSelector;
		}
	}
}